<template>
    <div class="app-container calendar-list-container">
        <!-- 查询和其他操作 -->
        <div class="filter-container">
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="支付通道名称" v-model="listQuery.payChannelName"></el-input>
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="支付通道编号" v-model="listQuery.payChannelName"></el-input>
            <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查询</el-button>
            <el-button class="filter-item" type="primary" v-waves icon="el-icon-plus" @click="handleAdd">新增</el-button>
        </div> 
        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
            <el-table-column align="center" min-width="100px" label="费率代码" prop="feeCode"></el-table-column>
            <el-table-column align="center" min-width="100px" label="费率名称" prop="feeName"></el-table-column>
            <el-table-column align="center" min-width="100px" label="币种" prop="ccy"></el-table-column>
            <el-table-column align="center" min-width="100px" label="计费方式">
                <template slot-scope="scope">
                    <span v-if="scope.row.billingMethod == '00'">固定金额</span>
                    <span v-if="scope.row.billingMethod == '01'">按比例</span>
                </template>
            </el-table-column>
            <el-table-column align="center" min-width="100px" label="收费比例" prop="feeRatio"></el-table-column>
            <el-table-column align="center" min-width="100px" label="最低收费" prop="minFee"></el-table-column>
            <el-table-column align="center" min-width="100px" label="最高收费" prop="maxFee"></el-table-column>
            <el-table-column align="center" min-width="100px" label="收费金额" prop="feeAmt"></el-table-column>
            <el-table-column align="center" label="操作" width="400" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="primary" size="small" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination-container" v-show="!listLoading">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 新增/修改 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form :rules="rules" ref="form" :model="form" status-icon label-position="left" label-width="120px" style='width: 400px; margin-left:50px;'>
                <el-form-item label="费率名称:" prop="feeName">
                    <el-input v-model="form.feeName"></el-input>
                </el-form-item>
                <el-form-item label="币种:" prop="ccy">
                    <el-select class="filter-item" v-model="form.ccy" placeholder="请选择">
                      <el-option label="人民币" value="ccy"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="计费方式： " prop="billingMethod">
                    <el-select class="filter-item" v-model="form.billingMethod" @change="onSelectedMethod($event)" placeholder="请选择">
                      <el-option label="固定金额" value="00"> </el-option>
                      <el-option label="按比例" value="01"> </el-option>
                    </el-select>
                </el-form-item>       
                <el-form-item label="收费金额:" prop="feeAmt" v-if="methodVisible != false">
                    <el-input v-model="form.feeAmt"></el-input>
                </el-form-item>
                <el-form-item label="收费比例:" prop="feeRatio" v-if="methodVisible == false">
                    <el-input v-model="form.feeRatio"></el-input>
                </el-form-item> 
                <el-form-item label="最低收费:" prop="minFee" v-if="methodVisible == false">
                    <el-input v-model="form.minFee"></el-input>
                </el-form-item>
                <el-form-item label="最高收费:" prop="maxFee" v-if="methodVisible == false">
                    <el-input v-model="form.maxFee"></el-input>
                </el-form-item> 
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button v-if="dialogStatus === 'create'" type="primary" @click="create('form')">保存</el-button>
                <el-button v-else-if="dialogStatus === 'update'" type="primary" @click="update('form')">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<style>

</style>

<script>
import { all, add, info, update, deleteObj } from '@/api/pay/channelFee'
import waves from '@/directive/waves' // 水波纹指令
export default {
  directives: { waves },
  data: function() {
    return {
      listQuery: {
        payChannelName: '',
        payChannelNo: '',
        page: 1,
        limit: 10
      },
      channel_btn_info: false,
      channel_btn_add: false,
      channel_btn_update: false,
      channel_btn_delete: false,
      channel_btn_interface: false,
      list: [],
      form: {
        feeName: '',
        ccy: '',
        billingMethod: '',
        feeAmt: '',
        feeRatio: '',
        minFee: '',
        maxFee: ''
      },
      form1: {
        payChannelNo: '',
        list: []
      },
      form2: {
        payChannelNo: '',
        tradeType: '',
        serviceName: ''
      },
      rules: {},
      total: undefined,
      listLoading: false,
      dialogFormVisible: false,
      dialogInterface: false,
      dialogInterfaceAdd: false,
      dialogStatus: '',
      interfaceDialogStatus: '',
      methodVisible: false,
      textMap: {
        update: '编辑',
        create: '创建',
        interface: '接口配置'
      }
    }
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleAdd() {
      this.resetTemp1()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      all(this.listQuery)
        .then(response => {
          console.log(response)
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    onSelectedMethod(event) {
      console.log('vvvvvvvvvvvvvvvv:' + this.form.billingMethod)
      if (this.form.billingMethod === '00') {
        this.methodVisible = true
        this.form.feeAmt = ''
      } else if (this.form.billingMethod === '01') {
        this.methodVisible = false
        this.form.feeRatio = ''
        this.form.minFee = ''
        this.form.maxFee = ''
      }
    },
    handleUpdate(row) {
      info(row.feeCode)
        .then(response => {
          this.dialogStatus = 'update'
          this.dialogFormVisible = true
          this.form = response
          if (response.billingMethod === '00') {
            this.methodVisible = true
            this.form.feeAmt = response.feeAmt
          } else if (response.billingMethod === '01') {
            this.methodVisible = false
            this.form.feeRatio = response.feeRatio
            this.form.minFee = response.minFee
            this.form.maxFee = response.maxFee
          }
        })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除选中支付通道, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteObj(row.feeCode)
            .then(response => {
              if (response.code === '0000') {
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              } else {
                this.$notify({
                  title: '失败',
                  message: response.msg,
                  type: 'faile',
                  duration: 2000
                })
                this.listLoading = false
              }
            })
        })
    },
    create(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm('此操作将添加支付通道, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.listLoading = true
            this.dialogFormVisible = false
            add(this.form).then((response) => {
              if (response.code === '0000') {
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 5000
                })
                this.getList()
                this.listLoading = false
              } else {
                this.$notify({
                  title: '失败',
                  message: response.msg,
                  type: 'faile',
                  duration: 2000
                })
                this.listLoading = false
              }
            })
            this.resetTemp1()
          })
        } else {
          return false
        }
      })
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          update(this.form).then(response => {
            if (response.code === '0000') {
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            } else {
              this.$notify({
                title: '失败',
                message: response.msg,
                type: 'faile',
                duration: 2000
              })
            }
          })
        } else {
          return false
        }
      })
    },
    resetTemp1() {
      this.form = {
      }
    }
  },
  created() {
    this.getList()
  }
}
</script>

